package com.shell.scheduler;

import com.shell.bee.base.utils.DateUtil;
import com.shell.scheduler.job.JobUtil;
import com.shell.scheduler.job.po.SchdJob;
import java.io.Serializable;
import java.util.Map;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.quartz.Job;
import org.quartz.JobDataMap;
import org.quartz.JobDetail;
import org.quartz.JobExecutionContext;

public class JobExecutor implements Job, Serializable {
	private static final long serialVersionUID = 3743773327754795320L;
	private final Log logger = LogFactory.getLog(getClass());

	public void execute(JobExecutionContext context) {
		this.logger.debug("Starting Time:" + DateUtil.getSystemMillTime());
		long startingTime = System.currentTimeMillis();

		JobDataMap jobDataMap = context.getJobDetail().getJobDataMap();
		SchdJob job = (SchdJob) jobDataMap.get("SCHE_JOB");
		Map<String, String> params = (Map) jobDataMap.get("JOB_PARAMS");

		String jobCode = job.getJobCode();
		Executor executor = JobUtil.getJobExecutor(jobCode, job.getJobExecutor());
		executor.execute(params);
		long endTime = System.currentTimeMillis();
		this.logger.debug(
				"End Time:" + DateUtil.getSystemMillTime() + "      Elapsed time:" + (endTime - startingTime) + "ms");
	}
}
